package com.caiyouxi.system.service.impl;

import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.cache.RedisService;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.common.util.Log;
import com.caiyouxi.system.mapper.SysLogBeanMapper;
import com.caiyouxi.system.model.SysLogBean;
import com.caiyouxi.system.service.SysLogBeanService;
import com.caiyouxi.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SysLogBeanServiceImpl extends BaseService implements SysLogBeanService{
    @Autowired
    private SysLogBeanMapper mapper;
    @Autowired
    private RedisService redisService;

    @Override
    public ResData delete_sys_log(SysLogBean bean) {
        return success(mapper.delete_sys_log(bean.getId()));
    }

    @Override
    public ResData insert_sys_log(SysLogBean bean) {
        return success(mapper.insert_sys_log(bean));
    }

    @Override
    public ResData select_single_syslog(SysLogBean bean) {
        return success(mapper.select_single_syslog(bean.getId()));
    }

    @Override
    public ResData update_syslog(SysLogBean bean) {
        return success(mapper.update_syslog(bean));
    }

    @Override
    public ResData get_all_syslog(SysLogBean bean) {
        Integer pageSize = Integer.parseInt(redisService.get("sys.pagesize").toString());//后台获取默认每页多少调数据
        QueryParams pageList = new QueryParams();
        Integer p = bean.getPage_size();                            //查看前台是否给了每页几条数据
        if(p == 0 || p == null){
            p = pageSize;                                           //如果没给数据，则取默认数据
        }
        bean.setPage_size(p);
        pageList.setPage(bean.getPage());
        pageList.setList(mapper.get_all_syslog(bean));
        pageList.setTotal(mapper.get_count_syslog(bean));
        return success(pageList);
    }

    /**
     * 日志服务
     * @param action
     */
    @Override
    public void add_mess_log(String target_id, String target_type, String user_id,String result_data,
                             String raw_data,String level,String product_code,String app_id, String action){
        Log.e("=========================order订单详细信息=============，get_order:");
        Log.e("调日志记录存储开始");
//        String target_id = bean.getId() == null ? "" : bean.getId().toString(); //对应目标对象的编号。如果 user 模块，那这里就是相关的用户编号
//        String target_type = "order";//目标对象类型，比如用户注册，目标对象就是 USER，具体的定义待定
////        String action = ;//动作。比如：CREATE、UPDATE、DELETE、VIEW LOGIN
//        String user_id = bean.getApp_user_id() == null ? "" : bean.getApp_user_id().toString();
//        String result_data = "";//目标数据。保存目标修改后的结果数据
//        String raw_data = "";//	原始数据。保存对应目标操作修改前的数据
//        String level = "";//日志级别：DEBUG,INFO,NOTICE,WARNING,ERROR,CRITICAL,ALERT
//        String product_code = "";//产品线代码。通过产品线来区分日志
//        String host_info = "sdkapi";//日志源信息，比如哪个应用
        LogUtil.i(target_id, target_type, action,user_id,result_data,raw_data,level,product_code,app_id);
        Log.e("调日志记录存储结束");
    }
}
